import router from './index';

const modules = import.meta.glob('../**/**/*.vue')

function isURL (s) {
    return /^http[s]?:\/\/.*/.test(s)
}
export const initDynamicRoutes = (menuData = [])=>{


    // 递归处理路由
    const formatRoutes = (aMenu = [], first)=> {
        const aRouter = []
        const propsDefault ={
            iconDefault: 'icon-caidan',
            label: 'label',
            path: 'fullPath',
            icon: 'icon',
            children: 'children',
            query: 'query',
            href: 'href',
            meta: 'meta'
        }
        if (aMenu && aMenu.length === 0) return;
        for (let i = 0; i < aMenu.length; i++) {
            const oMenu = aMenu[i];
            let path = oMenu[propsDefault.path],
                component = oMenu.component,
                name = oMenu[propsDefault.label],
                icon = oMenu[propsDefault.icon],
                children = oMenu[propsDefault.children],
                query = oMenu[propsDefault.query],
                meta = oMenu[propsDefault.meta],
                is_component = true;

            const isChild = !!(children && children.length !== 0);
            const oRouter = {
                path: path,
                component: (() => {
                    // 判断是否为首路由
                    if (first) {
                        return modules[ '../views/home/index.vue']
                        // 判断是否为多层路由
                    } else if (isChild && !first) {
                        return modules['../page/layout/index.vue']
                        // 判断是否为最终的页面视图
                    } else {
                        let result = modules[`../${component}.vue`]
                        if (result) result().then(mod => mod.default.name = path)
                        else {
                            is_component = false;
                            console.log(component + '不存在')
                        }
                        return result
                    }
                })(),
                name,
                icon,
                meta,
                query,
                redirect: (() => {
                    if (!isChild && first) return `${path}`
                    else return '';
                })(),
                // 处理是否为一级路由
                children: !isChild ? (() => {
                    if (first) {
                        oMenu[propsDefault.path] = `${path}`;
                        let result = modules[`../${component}.vue`]
                        if (result) result().then(mod => mod.default.name = path)
                        else {
                            is_component = false
                            console.log(component + '不存在')
                        }
                        return [{
                            component: result,
                            icon: icon,
                            name: name,
                            meta: meta,
                            query: query,
                            path: ''
                        }]
                    }
                    return [];
                })() : (() => {
                    return formatRoutes(children, false)
                })()
            }
            if (!isURL(path) && is_component) aRouter.push(oRouter)

        }

        if (first) {
            aRouter.forEach((ele) => router.addRoute(ele))
        } else {
            return aRouter
        }

    }

    formatRoutes(menuData,true)
}